package cn.IT.service.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.jasper.tagplugins.jstl.core.If;
import org.junit.internal.runners.model.EachTestNotifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.IT.dao.IReplyDAO;
import cn.IT.dao.ITopicDAO;
import cn.IT.dao.IUserDAO;
import cn.IT.domain.Reply;
import cn.IT.domain.Topic;
import cn.IT.domain.Type;
import cn.IT.domain.User;
import cn.IT.service.ITopicService;
import cn.IT.service.IUserService;
import net.sf.json.JSONObject;

@Service(value = "topicService")
@SuppressWarnings("all")
@Transactional
public class TopicService implements ITopicService {

	@Resource
	private ITopicDAO topicDao;
	@Resource
	private IUserDAO userDao;
	@Resource
	private IReplyDAO replyDao;

	@Override
	public List<Topic> getAllTopics() {
		List<Topic> topics = topicDao.findAll(Topic.class);
		if(topics != null){
			Collections.reverse(topics);
		}
		return topics;
	}
	
	@Override 
	public String delTopic(Topic topic,User user){
		User updateuser;
		try {
			
			//更新用户的评论数量
			List<Reply> reply = replyDao.findByTopicId(topic.getTopicId());
			if(reply != null){
			  for(int i = 0; i<reply.size();i++){
				  updateuser = reply.get(i).getUser();
				  updateuser.setReplyCount(updateuser.getReplyCount()-1);
				  userDao.update(updateuser);
			  }
			}
			
            //更新用户的帖子数量
			updateuser = userDao.get(User.class, user.getUserId());
			if(updateuser != null){
				updateuser.setTopicCount(updateuser.getTopicCount()-1);
				userDao.update(updateuser);
			}

			topicDao.delete(Topic.class, topic.getTopicId());
			return "success";
		} catch (Exception e) {
			System.out.println(e);
			return "error";
		}
	}
	
	@Override
	public Topic isTitleExist(Topic topic){
		return topicDao.isTitleExist(topic);
	}
	
	@Override
	public void addTopic(Topic topic){
		topic.setIsReaded(0);
		topic.setReplyCount(0);
		topic.setCreatedOn(new Date());
		topicDao.save(topic);
		User user = userDao.get(User.class, topic.getUserByUserId().getUserId());
		user.setTopicCount(user.getTopicCount()+1);
		userDao.update(user);
	}

	@Override
	public List<Topic> searchByTitle(String searchContent) {
		List<Topic> topics = topicDao.findByTitle(searchContent);
		if(topics != null){
			Collections.reverse(topics);
		}
		return topics;
		
	}
	
	@Override
	public List<Topic> searchByType(String searchContent) {
		List<Topic> topics = topicDao.findByType(searchContent);
		if(topics != null){
			Collections.reverse(topics);
		}
		return topics;
		
	}
	
	@Override
	public List<Topic> findAllTopicsByUser(User user) {
		List<Topic> topics = topicDao.findByUser(user);
		if(topics != null){
			Collections.reverse(topics);
		}
		return topics;
		
	}

	@Override
	public Topic getTopicById(Serializable topicId) {
		return topicDao.get(Topic.class, topicId);
	}
	
	@Override
	public List<Topic> getNewsByToUser(User user){
		List<Topic> topics = topicDao.findNewsByToUser(user);
		if(topics != null){
			Collections.reverse(topics);
		}
		return topics;
	}
	
	@Override
	public String updateNewsByToUser(User user){
		List<Topic> topics = topicDao.findNewsByToUser(user);
		Topic updateTopic;
		try {
			if(topics != null){
			   for(int i=0;i<topics.size();i++){
				   updateTopic = topics.get(i);
				   if (updateTopic.getIsReaded() == 0){
					   updateTopic.setIsReaded(1);
					   topicDao.update(updateTopic);
				   }
			   }
			}
			return "success";
		} catch (Exception e) {
			return "error";
		}	
	}
	

}
